import { Http } from '@angular/http';
import { Injectable } from '@angular/core';

import { ToastController, LoadingController, Platform, Loading, AlertController } from "ionic-angular";
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from "@ionic-native/splash-screen";
import { UrlConstant } from '../../commons/url';
import { Network } from '@ionic-native/network';

/*
  Generated class for the NavtiveServeProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class NavtiveServeProvider {
  public loading: Loading
  private loadingIsOpen: boolean = false;
  constructor(
    public http: Http,
    public ToasctCtrl: ToastController,
    public LoadingCtrl: LoadingController,
    public platform: Platform,
    public AlertCtrl: AlertController,
    public statusBar: StatusBar,
    public splashScreen: SplashScreen,
    public url: UrlConstant,
    public network: Network,
  ) {
    console.log('Hello NavtiveServeProvider Provider');
  }
  /**
  * 使用默认状态栏
  */
  statusBarStyleDefault(): void {
    this.statusBar.styleDefault();
  }
  /**
  * 隐藏启动页面
  */
  splashScreenHide(): void {
    this.splashScreen.hide();
  }

  /**
  * 是否真机环境
  */
  isMobile(): boolean {
    return this.platform.is('mobile') && !this.platform.is('mobileweb');
  }
  /**
    * 是否android真机环境
    */
  isAndroid(): boolean {
    return this.isMobile() && this.platform.is('android');
  }

  /**
   * 是否ios真机环境
   */
  isIos(): boolean {
    return this.isMobile() && (this.platform.is('ios') || this.platform.is('ipad') || this.platform.is('iphone'));
  }
  /**
   * Alert 弹框
   * @param title 
   * @param subTitle 
   */
  alert(title: string, subTitle: string = "", ): void {
    this.AlertCtrl.create({
      title: title,
      subTitle: subTitle,
      buttons: [{ text: '确定' }]
    }).present();
  }
  /**
   * 统一调用此方法显示提示信息
   * @param message 信息内容
   * @param duration 显示时长
   */
  showToast(message: string = '操作完成', duration: number = 2000): void {
    this.ToasctCtrl.create({
      message: message,
      duration: duration,
      position: 'middle',
      showCloseButton: false,
      cssClass: 'showStyle',
    }).present();
  }

  showLoading(content: string = '加载中'): void {
    if (!this.loadingIsOpen) {
      this.loadingIsOpen = true;
      this.loading = this.LoadingCtrl.create({
        content: content
      });
      this.loading.present();
      setTimeout(() => {
        this.loadingIsOpen && this.loading.dismiss();
        this.loadingIsOpen = false;
      }, 1000);
    }
  };
  /**
  * 关闭loading
  */
  hideLoading(): void {
    this.loadingIsOpen && this.loading.dismiss();
    this.loadingIsOpen = false;
  };

    /**
   * 获取网络类型 如`unknown`, `ethernet`, `wifi`, `2g`, `3g`, `4g`, `cellular`, `none`
   */
  getNetworkType(): string {
    if (!this.isMobile()) {
      return 'wifi';
    }
    return this.network.type;
  }
   /**
   * 判断是否有网络
   */
  isConnecting(): boolean {
    return this.getNetworkType() != 'none';
  }
}
